import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:ichinesereader/models/common/AccessCodeResetPasswordDto.dart';
import 'package:ichinesereader/models/common/PropertyMessageResult.dart';
import 'package:ichinesereader/service/user.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:reactive_forms/reactive_forms.dart';
import 'package:get/get.dart';

class ResetPasswordByIdentifierTab extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Log.debug('build ResetPasswordByIdentifierTab');

    UserServiceProvider _userservice = new UserServiceProvider();
    final form = FormGroup({
      'identitycode': FormControl<String>(validators: [Validators.required]),
      'username': FormControl<String>(validators: [Validators.required]),
      'password': FormControl<String>(validators: [Validators.required]),
    });

    void _resetPasswordBySecurityCode(AccessCodeResetDto dto) async {
      try {
        EasyLoading.show();
        var response =
            await _userservice.resetPasswordBySecurityIdentifier(dto);
        if (response.statusCode == 200) {
          showSnackMsg('Password has been reset successfully.');
          // Get.snackbar('Message'.tr, 'Password has been reset successfully.'.tr,
          //     backgroundColor: Theme.of(context).primaryColor,
          //     colorText: Theme.of(context).primaryColorLight,
          //     snackPosition: SnackPosition.BOTTOM);
        } else {
          var result = propertyMessageResultFromJson(response.body);
          showSnackMsg('${result.message?.message.toString()}');
          // Get.snackbar('Message'.tr, '${result.message?.message.toString()}'.tr,
          //     backgroundColor: Theme.of(context).primaryColor,
          //     colorText: Theme.of(context).primaryColorLight,
          //     snackPosition: SnackPosition.BOTTOM);
        }
      } catch (e) {
      } finally {
        EasyLoading.dismiss();
      }
    }

    return Padding(
      padding: const EdgeInsets.fromLTRB(50, 36, 50, 18),
      child: ReactiveForm(
        formGroup: form,
        child: Column(
          children: <Widget>[
            ReactiveTextField(
              formControlName: 'identitycode',
              textInputAction: TextInputAction.next,
              style: Theme.of(context).textTheme.titleSmall,
              onSubmitted: (_) => form.focus('username'),
              decoration: InputDecoration(
                labelText: 'Security Identifier'.tr,
              ),
            ),
            SizedBox(
              height: 12,
            ),
            ReactiveTextField(
              formControlName: 'username',
              textInputAction: TextInputAction.next,
              style: Theme.of(context).textTheme.titleSmall,
              onSubmitted: (_) => form.focus('password'),
              decoration: InputDecoration(
                alignLabelWithHint: true,
                labelText: 'User Name'.tr,
              ),
            ),
            SizedBox(
              height: 12,
            ),
            ReactiveTextField(
              formControlName: 'password',
              textInputAction: TextInputAction.done,
              style: Theme.of(context).textTheme.titleSmall,
              obscureText: true,
              decoration: InputDecoration(
                alignLabelWithHint: true,
                labelText: 'New Password'.tr,
              ),
            ),
            SizedBox(
              height: 12,
            ),
            ReactiveFormConsumer(
              builder: (context, form, child) {
                AccessCodeResetDto dto = new AccessCodeResetDto();
                dto.accessCode = form.control('identitycode').value;
                dto.userName = form.control('username').value;
                dto.newPassword = form.control('password').value;
                return ElevatedButton(
                    style: ButtonStyle(
                      minimumSize: MaterialStateProperty.all(
                        Size(200, 50),
                      ),
                      backgroundColor: MaterialStateProperty.all(
                        Color(0xff0085cc),
                      ),
                    ),
                    child: Text('Submit'.tr,
                        style: TextStyle(
                          fontSize: 18.0,
                        )),
                    onPressed: () {
                      if (form.valid) {
                        _resetPasswordBySecurityCode(dto);
                      }
                    });
              },
            ),
          ],
        ),
      ),
    );
  }
}
